package com.yzt.zhuju.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.IService.chatroom.ChatRoomMsgType;
import com.yzt.zhuju.network.entity.chatroom.ChatRoomChatMessage;
import com.yzt.zhuju.network.entity.chatroom.ChatRoomGiftDetail;
import com.yzt.zhuju.network.entity.chatroom.ChatRoomManagerInfo;
import com.yzt.zhuju.rongyun.ChatRoomMessage;
import com.yzt.zhuju.rongyun.ChatRoomMsgBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   :
 */
public class RongYunOnReceiverMsgManager {

    public static List<RongYunMsgObservable> mRongYunMsgObservables = new ArrayList<>();

    public interface RongYunMsgObservable {
        void onChatRoomMsgReceiver(ChatRoomMessage mChatRoomMessage);
    }

    private RongYunOnReceiverMsgManager() {

    }

    static class InstanceHolder {
        final static RongYunOnReceiverMsgManager INSTANCE = new RongYunOnReceiverMsgManager();
    }

    public static RongYunOnReceiverMsgManager getInstance() {
        return RongYunOnReceiverMsgManager.InstanceHolder.INSTANCE;
    }

    public void registerRongSendChangedObservable(RongYunOnReceiverMsgManager.RongYunMsgObservable observable) {
        if (null != observable) {
            if (!mRongYunMsgObservables.contains(observable)) {
                mRongYunMsgObservables.add(observable);
            }
        }
    }

    public void unRegisterRongSendChangedObservable(RongYunOnReceiverMsgManager.RongYunMsgObservable observable) {
        if (null != observable) {
            mRongYunMsgObservables.remove(observable);
        }
    }

    public void spreadChatRoomMessage(ChatRoomMessage mChatRoomMessage) {

        onChatRoomMsgReceiverComputing(new ChatRoomMessage(mChatRoomMessage.type, mChatRoomMessage.msgId, mChatRoomMessage.content,
                mChatRoomMessage.extra, mChatRoomMessage.deltaTime, mChatRoomMessage.targetId));
        for (RongYunOnReceiverMsgManager.RongYunMsgObservable observable : mRongYunMsgObservables) {
            observable.onChatRoomMsgReceiver(mChatRoomMessage);
        }
    }

    public void spreadChatRoomMessageNotAddToCache(ChatRoomMessage mChatRoomMessage) {
        for (RongYunOnReceiverMsgManager.RongYunMsgObservable observable : mRongYunMsgObservables) {
            observable.onChatRoomMsgReceiver(mChatRoomMessage);
        }
    }

    private void onChatRoomMsgReceiverComputing(ChatRoomMessage mChatRoomMessage) {
        switch (mChatRoomMessage.type) {
            case ChatRoomMsgType.TYPE_SIMPLE_TEXT:
            case ChatRoomMsgType.TYPE_PHOTO:
            case ChatRoomMsgType.TYPE_MAGIC_CHANGE:
                break;
            case ChatRoomMsgType.TYPE_EMOIJ_BIG:
//                ChatRoomEmoijData mChatRoomEmoijData = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomEmoijData.class);
//                mChatRoomMessage.content = mChatRoomEmoijData.getText();
//                ChatRoomMsgBean mChatRoomMsgBean = getChatRoomMsgBean(mChatRoomMessage);
//                mChatRoomMsgBean.setGameResult(mChatRoomEmoijData.getGameResult());
                break;
            case ChatRoomMsgType.TYPE_SEAT_UN_SPEAK:
                onSpeakStatus(mChatRoomMessage);
                break;
            case ChatRoomMsgType.TYPE_SEAT_CLOSE:
                onSeatStatus(mChatRoomMessage);
                break;
            case ChatRoomMsgType.TYPE_SEAT_OPEN_MUSIC:
                onMusicStatus(mChatRoomMessage);
                break;
            case ChatRoomMsgType.TYPE_SET_MANAGER:
                //设为管理/取消管理
                onSetOrCancelManager(mChatRoomMessage);
                break;
            case ChatRoomMsgType.TYPE_RED_BAG:
                onRedBagStatus(mChatRoomMessage);
                break;
            case ChatRoomMsgType.TYPE_CHATROOM_GIFT:
            case ChatRoomMsgType.TYPE_CHATROOM_GIFT_ALL:
                onChatRoomGift(mChatRoomMessage);
                break;
            default:
                break;
        }
    }

    private ChatRoomMsgBean getChatRoomMsgBean(ChatRoomMessage mChatRoomMessage) {
        String content = mChatRoomMessage.content;
        String extra = mChatRoomMessage.extra;
        ChatRoomChatMessage mChatRoomChatMessage = JSONObject.parseObject(extra, ChatRoomChatMessage.class);
        ChatRoomMsgBean mChatRoomMsgBean = new ChatRoomMsgBean();
        mChatRoomMsgBean.setType(mChatRoomMessage.type);
        mChatRoomMsgBean.setMsgId(mChatRoomMessage.msgId);
        mChatRoomMsgBean.setText(content);
        mChatRoomMsgBean.setChatRoomChatMessage(mChatRoomChatMessage);
        return mChatRoomMsgBean;
    }

    private void onSpeakStatus(ChatRoomMessage mChatRoomMessage) {
//        ChatRoomSpeakInfo mChatRoomSpeakInfo = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomSpeakInfo.class);
//        mChatRoomMessage.content = String.format(!mChatRoomSpeakInfo.isStatus() ? AppConfig.context.getString(R.string.open_seat_mic) : AppConfig.context.getString(R.string.stop_seat_mic), mChatRoomSpeakInfo.getSeatId() + 1);
    }

    /**
     * 座位权限
     */
    private void onSeatStatus(ChatRoomMessage mChatRoomMessage) {
//        ChatRoomSpeakInfo mChatRoomSpeakInfo = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomSpeakInfo.class);
//        mChatRoomMessage.content = String.format(!mChatRoomSpeakInfo.isStatus() ? AppConfig.context.getString(R.string.open_seat_status) : AppConfig.context.getString(R.string.close_seat_status), mChatRoomSpeakInfo.getSeatId() + 1);
    }

    /**
     * 音乐权限
     */
    private void onMusicStatus(ChatRoomMessage mChatRoomMessage) {
//        ChatRoomSpeakInfo mChatRoomSpeakInfo = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomSpeakInfo.class);
//        mChatRoomMessage.content = String.format(mChatRoomSpeakInfo.isStatus() ? AppConfig.context.getString(R.string.open_seat_music) : AppConfig.context.getString(R.string.close_seat_music), mChatRoomSpeakInfo.getSeatId() + 1);
    }

    /**
     * 设置或取消管理员
     */
    private void onSetOrCancelManager(ChatRoomMessage mChatRoomMessage) {
        ChatRoomManagerInfo mChatRoomManagerInfo = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomManagerInfo.class);
        mChatRoomMessage.content = mChatRoomManagerInfo.getContent();
    }

    private void onRedBagStatus(ChatRoomMessage mChatRoomMessage) {
//        ChatRoomRedBagBean mChatRoomRedBagBean = JSONObject.parseObject(mChatRoomMessage.content, ChatRoomRedBagBean.class);
//        mChatRoomMessage.content = mChatRoomRedBagBean.getNickName();
//        ChatRoomMsgBean mChatRoomMsgBean = getChatRoomMsgBean(mChatRoomMessage);
//        mChatRoomMsgBean.setChatRoomRedBagBean(mChatRoomRedBagBean);
    }

    private void onChatRoomGift(ChatRoomMessage mChatRoomMessage) {
        String ext = mChatRoomMessage.content;
        String extra = mChatRoomMessage.extra;
        ChatRoomChatMessage mChatRoomChatMessage = JSONObject.parseObject(extra, ChatRoomChatMessage.class);
        String mString = (com.alibaba.fastjson.JSONObject.toJSON(ext).toString());
        ChatRoomGiftDetail mChatRoomGiftDetail = JSON.parseObject(mString, ChatRoomGiftDetail.class);

        ChatRoomMsgBean mChatRoomMsgBean = new ChatRoomMsgBean();
        mChatRoomMsgBean.setType(mChatRoomMessage.type);
        mChatRoomMsgBean.setMsgId(mChatRoomMessage.msgId);
        mChatRoomMsgBean.setChatRoomGiftDetail(mChatRoomGiftDetail);
        mChatRoomMsgBean.setChatRoomChatMessage(mChatRoomChatMessage);
    }

}
